using System;
using System.Collections.Generic;

namespace SubversionReports.Entities
{
    public class PathSummary
    {
        private readonly string fileName;
        private readonly bool isFolder;
        private readonly int firstRevisionId;
        private readonly int lastRevisionId;
        private readonly DateTime firstRevisionDate;
        private readonly DateTime lastRevisionDate;
        private readonly string firstRevisionAuthor;
        private readonly string lastRevisionAuthor;
        private readonly PathRevisionType lastRevisionType;

        /// <summary>
        /// Gets the name of the file.
        /// </summary>
        /// <value>The name of the file.</value>
        public string FileName
        {
            get { return fileName; }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is folder.
        /// </summary>
        /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
        public bool IsFolder
        {
            get { return isFolder; }
        }

        /// <summary>
        /// Gets the first revision id.
        /// </summary>
        /// <value>The first revision id.</value>
        public int FirstRevisionId
        {
            get { return firstRevisionId; }
        }

        /// <summary>
        /// Gets the last revision id.
        /// </summary>
        /// <value>The last revision id.</value>
        public int LastRevisionId
        {
            get { return lastRevisionId; }
        }

        /// <summary>
        /// Gets the first revision date.
        /// </summary>
        /// <value>The first revision date.</value>
        public DateTime FirstRevisionDate
        {
            get { return firstRevisionDate; }
        }

        /// <summary>
        /// Gets the last revision date.
        /// </summary>
        /// <value>The last revision date.</value>
        public DateTime LastRevisionDate
        {
            get { return lastRevisionDate; }
        }

        /// <summary>
        /// Gets the first revision author.
        /// </summary>
        /// <value>The first revision author.</value>
        public string FirstRevisionAuthor
        {
            get { return firstRevisionAuthor; }
        }

        /// <summary>
        /// Gets the last revision author.
        /// </summary>
        /// <value>The last revision author.</value>
        public string LastRevisionAuthor
        {
            get { return lastRevisionAuthor; }
        }

        /// <summary>
        /// Gets the type of the last revision.
        /// </summary>
        /// <value>The type of the last revision.</value>
        public PathRevisionType LastRevisionType
        {
            get { return lastRevisionType; }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get
            {
                string description = firstRevisionId.Equals(0) ? string.Empty : string.Format("Added by {0} on {1}", firstRevisionAuthor ?? "unknown author", firstRevisionDate.Equals(new DateTime()) ? "unknown date" : firstRevisionDate.ToShortDateString());
                if (!lastRevisionType.Equals(PathRevisionType.Unknown) && lastRevisionId > firstRevisionId)
                    description += string.Format("; {0} by {1} on {2:dd/MM/yyyy}", lastRevisionType, lastRevisionAuthor, lastRevisionDate);
                return description;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PathSummary"/> class.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="isFolder">if set to <c>true</c> [is folder].</param>
        /// <param name="firstRevisionId">The first revision id.</param>
        /// <param name="lastRevisionId">The last revision id.</param>
        /// <param name="firstRevisionDate">The first revision date.</param>
        /// <param name="lastRevisionDate">The last revision date.</param>
        /// <param name="firstRevisionAuthor">The first revision author.</param>
        /// <param name="lastRevisionAuthor">The last revision author.</param>
        /// <param name="lastRevisionType">Type of the last revision.</param>
        public PathSummary(string fileName, bool isFolder, int firstRevisionId, int lastRevisionId, DateTime firstRevisionDate, DateTime lastRevisionDate, string firstRevisionAuthor, string lastRevisionAuthor, PathRevisionType lastRevisionType)
        {
            this.fileName = fileName;
            this.isFolder = isFolder;
            this.firstRevisionId = firstRevisionId;
            this.lastRevisionId = lastRevisionId;
            this.firstRevisionDate = firstRevisionDate;
            this.lastRevisionDate = lastRevisionDate;
            this.firstRevisionAuthor = firstRevisionAuthor;
            this.lastRevisionAuthor = lastRevisionAuthor;
            this.lastRevisionType = lastRevisionType;
        }
    }

    public class PathSummaries : List<PathSummary>
    {
        /// <summary>
        /// Determines whether [contains] [the specified path].
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// 	<c>true</c> if [contains] [the specified path]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string path)
        {
            return Find(delegate(PathSummary pathSummary) { return pathSummary.FileName.Equals(path); }) != null;
        }

        /// <summary>
        /// Adds the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="isFolder">if set to <c>true</c> [is folder].</param>
        /// <param name="firstRevisionId">The first revision id.</param>
        /// <param name="lastRevisionId">The last revision id.</param>
        /// <param name="firstRevisionDate">The first revision date.</param>
        /// <param name="lastRevisionDate">The last revision date.</param>
        /// <param name="firstRevisionAuthor">The first revision author.</param>
        /// <param name="lastRevisionAuthor">The last revision author.</param>
        /// <param name="lastRevisionType">Type of the last revision.</param>
        public void Add(string fileName, bool isFolder, int firstRevisionId, int lastRevisionId, DateTime firstRevisionDate, DateTime lastRevisionDate, string firstRevisionAuthor, string lastRevisionAuthor, PathRevisionType lastRevisionType)
        {
            Add(new PathSummary(fileName, isFolder, firstRevisionId, lastRevisionId, firstRevisionDate, lastRevisionDate, firstRevisionAuthor, lastRevisionAuthor, lastRevisionType));
        }
    }
}
